package com.ukao.pad.dialog;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.mcxtzhang.commonadapter.rv.OnItemClickListener;
import com.ukao.pad.R;
import com.ukao.pad.adapter.AddBatchDialogAdapter;
import com.ukao.pad.base.BaseDialogFragment;
import com.ukao.pad.base.IAdapter;
import com.ukao.pad.base.IPage;
import com.ukao.pad.base.PageWrapper;
import com.ukao.pad.bean.BatchlistBean;
import com.ukao.pad.bean.StringBean;
import com.ukao.pad.consts.Constant;
import com.ukao.pad.consts.SaveParamets;
import com.ukao.pad.listener.MyOnItemClickListener;
import com.ukao.pad.retrofit.ApiCallback;
import com.ukao.pad.utils.CheckUtils;
import com.ukao.pad.utils.T;
import com.ukao.pad.utils.Utils;
import com.ukao.pad.widget.ClearEditText;
import com.ukao.pad.widget.MyLRecyclerView;
import com.ukao.pad.widget.PercentLinearLayout;
import com.ukao.pad.widget.StateButton;
import com.ukao.pad.widget.StateImageView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 添加衣物
 */
public class AddBatchDialogtFragment extends BaseDialogFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    Unbinder unbinder;

    @BindView(R.id.search_edit_left)
    ClearEditText searchEditLeft;
    @BindView(R.id.search_button)
    StateImageView searchButton;
    @BindView(R.id.left_search_layout)
    LinearLayout leftSearchLayout;
    @BindView(R.id.add_clothing_layout)
    PercentLinearLayout addClothingLayout;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.cancel)
    StateButton cancel;
    @BindView(R.id.submit)
    Button submit;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private AddBatchDialogAdapter mAddBatchDialogAdapter;
    private ArrayList<BatchlistBean> mStationData;
    boolean isAddClothing=false;

    private String outId;


    public static AddBatchDialogtFragment newInstance(String outId, String param2) {
        AddBatchDialogtFragment fragment = new AddBatchDialogtFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, outId);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            outId = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_add_batch_dialogt, container, false);
        unbinder = ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initListener();

    }

    @Override
    protected void initView() {
        super.initView();

        mStationData = new ArrayList<>();
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        mAddBatchDialogAdapter = new AddBatchDialogAdapter(activity, mStationData);
        recyclerView.setAdapter(mAddBatchDialogAdapter);

    }



    @Override
    protected void initListener() {
        super.initListener();

        searchEditLeft.setOnEditorActionListener(mOnEditorActionListener);
    }



    TextView.OnEditorActionListener mOnEditorActionListener = new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (!CheckUtils.isNull(event) && event.getKeyCode() == KeyEvent.KEYCODE_ENTER || actionId == EditorInfo.IME_ACTION_DONE) {
                searchSanCode();

            }
            return false;
        }
    };



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        if (mOnConfirmClickListener!=null&&isAddClothing){
            mOnConfirmClickListener.onChildItemClick(Constant.START_COUNT_DESTROY,"");

        }
    }




    /**
     * 出厂添加衣物
     *
     * @param outId    出厂批次id
     * @param scanCode 衣物编号
     */
    public void addBatch(String outId, String scanCode) {
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("outId", outId);
        mHashMap.put("scanCode", scanCode);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores().addBatch(Constant.createParameter(mHashMap)),
                new ApiCallback<BatchlistBean>() {
                    @Override
                    public void onSuccess(BatchlistBean model) {

                        if (model.getHttpCode() == 200) {
                            searchEditLeft.setText("");
                            isAddClothing=true;
                            mAddBatchDialogAdapter.getDatas().add(model.getData());
                            mAddBatchDialogAdapter.notifyDataSetChanged();
                        } else {
                            T.show(model.getMsg());


                        }
                    }

                    @Override
                    public void onNoNewWork() {

                    }


                    @Override
                    public void onFinish() {

                    }
                });
    }

    @OnClick({R.id.cancel, R.id.submit,R.id.close_btn,R.id.search_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cancel:
                dismiss();

                break;
            case R.id.close_btn:
                dismiss();
                break;
            case R.id.submit:
                dismiss();

                break;
            case  R.id.search_button:
                searchSanCode();

                break;
        }
    }

    private void searchSanCode() {
        String   strSearchEditLeft=getText(searchEditLeft);
        if (!CheckUtils.isEmpty(strSearchEditLeft)){
            addBatch(outId,strSearchEditLeft);
        }
    }
}
